import 'package:app_settings/app_settings.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:inmotion_pro/l10n/local.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';

// 权限服务
class PermissionService {
  static PermissionService get to => Get.find();

  // 申请相册权限
  requestPhotoPermission(Function(bool) callBack) {
    SystemPermissionUtil.requestPermissions(
      [CustomPermissions.photos],
      onSuccess: () async {
        callBack(true);
        debugPrint('权限申请成功');
      },
      onCancel: () {
        callBack(false);
      },
      onFailed: (permission) {
        callBack(false);
        debugPrint('权限申请失败: $permission');
        LxAlert.show(
          local.you_have_been_permanently_denied_photo_permission,
          title: local.prompt_title,
          cancelTitle: local.cancel,
          confirmTitle: local.confirm,
          confirmCallback: (_) {
            // 打开系统设置
            AppSettings.openAppSettings();
          },
        );
      },
    );
  }

  // 申请相机权限
  requestCameraPermission(Function(bool) callBack) {
    SystemPermissionUtil.requestPermissions(
      [CustomPermissions.camera],
      onSuccess: () {
        debugPrint('权限申请成功');
        callBack(true);
      },
      onCancel: () {
        callBack(false);
      },
      onFailed: (permission) {
        callBack(false);
        debugPrint('权限申请失败: $permission');
        LxAlert.show(
          local.you_have_been_permanently_denied_camera_permission,
          title: local.prompt_title,
          cancelTitle: local.cancel,
          confirmTitle: local.confirm,
          confirmCallback: (_) {
            // 打开系统设置
            AppSettings.openAppSettings();
          },
        );
      },
    );
  }
}
